//
//  CZNewFeatureController.m
//  A01-CZ彩票
//
//  Created by apple on 15/6/17.
//  Copyright (c) 2015年 itcast. All rights reserved.
//
#define kCOUNT 4
#import "CZNewFeatureController.h"
#import "CZNewFeatureCell.h"
#import "UIView+Ex.h"
@interface CZNewFeatureController ()
@property (nonatomic, weak) UIImageView *guideView;
@property (nonatomic, weak) UIImageView *guideLargeTextView;
@property (nonatomic, weak) UIImageView *guideSmallTextView;
@end

@implementation CZNewFeatureController

//1 设置flowlayout
- (instancetype)init{
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    //设置cell的大小和间距
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    //修改滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    return [super initWithCollectionViewLayout:flowLayout];
}
static NSString *reuseId = @"image";
//2 注册nib或者自定义cell的类
- (void)viewDidLoad{
    [super viewDidLoad];
    
    //设置分页、取消弹簧效果、不显示横向滚动条
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    UINib *nib = [UINib nibWithNibName:@"CZNewFeatureCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:reuseId];
    
    //添加界面上的图片
    [self addImageView];
}

- (void)addImageView{
    //线
    UIImageView *guideLineView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLineView.x = -200;
    [self.collectionView addSubview:guideLineView];
    
    //添加参与动画的图片
    UIImageView *guideView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    self.guideView = guideView;
    [self.collectionView addSubview:guideView];
    
    UIImageView *guideLargeTextView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    self.guideLargeTextView = guideLargeTextView;
    guideLargeTextView.y = self.collectionView.height*0.7;
    [self.collectionView addSubview:guideLargeTextView];
    
    UIImageView *guideSmallTextView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    self.guideSmallTextView = guideSmallTextView;
    guideSmallTextView.y = self.collectionView.height * 0.8;
    [self.collectionView addSubview:guideSmallTextView];
}
//当滚动停止的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //横向偏移
    CGFloat offsetX = scrollView.contentOffset.x;
    //宽度
    CGFloat w = scrollView.width;
    
    //判断当前是往左滚动还是往右滚动
    if (offsetX < self.guideView.x) {
        w = -w;
    }
    
    
    //把图片移动到屏幕的最（右边）外面
    self.guideView.x = offsetX + w;
    self.guideLargeTextView.x = offsetX + w;
    self.guideSmallTextView.x = offsetX + w;
    //动画
    [UIView animateWithDuration:0.2 animations:^{
        self.guideView.x = offsetX;
        self.guideLargeTextView.x = offsetX;
        self.guideSmallTextView.x = offsetX;

    }];
    
    //修改图片
    int page = offsetX / abs((int)w) + 1;
    NSString *guide = [NSString stringWithFormat:@"guide%d",page];
    NSString *guideLargeText = [NSString stringWithFormat:@"guideLargeText%d",page];
    NSString *guideSmallText = [NSString stringWithFormat:@"guideSmallText%d",page];

    self.guideView.image = [UIImage imageNamed:guide];
    self.guideLargeTextView.image = [UIImage imageNamed:guideLargeText];
    self.guideSmallTextView.image = [UIImage imageNamed:guideSmallText];

    
}



//3 数据源的方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return kCOUNT;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //1
    
    CZNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseId forIndexPath:indexPath];
    //2
    //设置自定义cell的背景图片
    cell.imageName = [NSString stringWithFormat:@"guide%zdBackground",indexPath.item+1];
    
    //判断是否是最后一个cell，如果是得话显示startView
    if (indexPath.item == kCOUNT-1) {
        [cell setStartViewHidden:NO];
    }else{
        [cell setStartViewHidden:YES];
    }
    
    //3
    return cell;
    
}

@end
